//
//  TZSetTabConterller.m
//  wangYiLottery
//
//  Created by mac on 16/2/28.
//  Copyright © 2016年 tz. All rights reserved.
//

#import "TZSetController.h"
#import "TZSetCellModel.h"
#import "TZSetItemsModel.h"
#import "TZSetCell.h"
#import "TZMorePushController.h"
#import "TZNeteaseViewController.h"
#import "TZAboutViewController.h"
#import "UMSocial.h"

@interface TZSetController ()<UITableViewDataSource,UITableViewDelegate,UMSocialUIDelegate>
@property (nonatomic, strong) UITableView *setTableView;
@property (nonatomic, strong) NSArray *setArray;
@property (nonatomic, strong) UIView *coverView;
@property (nonatomic, strong) UIView *showView;
@end

@implementation TZSetController

//检测版本更新时：非最新版本->确定
- (void)okShowButtonDidClick {
    NSLog(@"%s",__FUNCTION__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationItem setTitle:@"设置"];
    [self setupUI];
}

//更新UI
- (void)setupUI {
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.setArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    TZSetCellModel *model = self.setArray[section];
    return model.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TZSetCell *cell = [TZSetCell settingCellWithTableView:tableView];
    cell.cellModel = self.setArray[indexPath.section];
    cell.itemsModel = [self.setArray[indexPath.section] items][indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    TZSetCellModel *model = self.setArray[section];
    return model.header;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self oldControllerToNewsControllerAddWithSection:indexPath.section addWithRow:indexPath.row];
}

- (void)oldControllerToNewsControllerAddWithSection:(NSInteger)Section addWithRow:(NSInteger)Row {
    if (Section == 0 && Row == 0) {
        TZMorePushController *morePushController = [[TZMorePushController alloc] init];
        [self.navigationController pushViewController:morePushController animated:YES];
    } else if (Section == 1 && Row == 0) {
        [self pickViewController];
        [self saveAppVersion];
        return;
    } else if (Section == 1 && Row == 1) {
        [self UMeng];
        return;
    } else if (Section == 1 && Row == 2) {
        TZNeteaseViewController *neteaseViewController = [[TZNeteaseViewController alloc] init];
        [self.navigationController pushViewController:neteaseViewController animated:YES];
    } else if (Section == 1 && Row == 3) {
        TZAboutViewController *aboutViewController = [[TZAboutViewController alloc] init];
        [self.navigationController pushViewController:aboutViewController animated:YES];
    } else {
//        NSLog(@"UISwitch");
        return;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    TZSetCellModel *model = self.setArray[section];
    return model.footer;
}

//重写初始化方法
- (instancetype)init {
    return [super initWithStyle:UITableViewStyleGrouped];
}
- (instancetype)initWithStyle:(UITableViewStyle)style {
    return [super initWithStyle:UITableViewStyleGrouped];
}

//友盟分享
- (void)UMeng{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"56fa8ef567e58eded800006e"
                                      shareText:@"网易彩票，愿你亿万梦想"
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ,UMShareToQzone,UMShareToEmail,nil]
                                       delegate:self];
}

//检查版本更新
//------------
//获取当前的版本号
- (NSString *)loadAppVersion {
    //获取info.plist
    NSDictionary *dict = [NSBundle mainBundle].infoDictionary;
    return dict[@"CFBundleShortVersionString"];
}

//把当前的版本号保存到沙盒中
- (void)saveAppVersion {
    //获取info.plist
    NSDictionary *dict = [NSBundle mainBundle].infoDictionary;
    //偏好设置
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setObject:dict[@"CFBundleShortVersionString"] forKey:@"AppVersion"];
}

//获取沙盒中保存的版本号
- (NSString *)loadSaveAppVersion {
    //获取偏好设置
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    return [ud objectForKey:@"AppVersion"];
}

//选择应该显示的控制器
- (void)pickViewController {
    //判断沙盒和当前的版本号是不是一致,一致则显示tabber，否则显示新特性页面(引导页)
    if ([[self loadSaveAppVersion] isEqualToString:[self loadAppVersion]] ) {
        UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"当前版本为最新版本" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:nil];
        [controller addAction:action];
        [self presentViewController:controller animated:YES completion:nil];
    } else {
        UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"当前版本不是最新版本" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //遮罩
            [self activity];
        }];
        [controller addAction:action];
        [self presentViewController:controller animated:YES completion:nil];
    }
}

- (void)activity {
    //cover(遮罩)
    UIView *coverView = [[UIView alloc] init];
    //遮罩的大小
    coverView.frame = KSCREEN_SIZE;
    coverView.backgroundColor = [UIColor blackColor];
    coverView.alpha = 0.5;
    [self.tabBarController.view addSubview:coverView];
    self.coverView = coverView;
    //更新提示View的背景图
    self.showView = [[UIView alloc] init];
    [self.tabBarController.view addSubview:self.showView];
    [self.showView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.tabBarController.view);
        make.width.mas_equalTo(KSCREEN_WIDTH * 0.5);
        make.height.mas_equalTo(KSCREEN_HEIGHT * 0.5);
    }];
    self.showView.backgroundColor = [UIColor redColor];
    //展示框的背景图
    UIImageView *showImageView = [[UIImageView alloc] init];
    [self.showView addSubview:showImageView];
    [showImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.showView);
    }];
    showImageView.image = [UIImage imageNamed:@"luck_entry_background"];
    //删除按钮
    UIButton *closeButton = [[UIButton alloc] init];
    [self.showView addSubview:closeButton];
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.showView);
        make.width.mas_equalTo(39 * 0.5);
        make.height.mas_equalTo(39 * 0.5);
    }];
    [closeButton setBackgroundImage:[UIImage imageNamed:@"Circle_DeleteButton"] forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(closeButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    //展示框提示更新Button
    //确定
    UIButton *okShowButton = [[UIButton alloc] init];
    [self.showView addSubview:okShowButton];
    [okShowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.showView);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(KSCREEN_WIDTH * 0.25);
    }];
    [okShowButton setTitle:@"确定" forState:UIControlStateNormal];
    [okShowButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [okShowButton setBackgroundImage:[UIImage imageNamed:@"CPArenaSegmentSelectedBG"] forState:UIControlStateNormal];
    [okShowButton addTarget:self action:@selector(okShowButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    //取消
    UIButton *noShowButton = [[UIButton alloc] init];
    [self.showView addSubview:noShowButton];
    [noShowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.showView);
        make.height.mas_equalTo(okShowButton.mas_height);
        make.width.mas_equalTo(KSCREEN_WIDTH * 0.25);
    }];
    [noShowButton setTitle:@"取消" forState:UIControlStateNormal];
    [noShowButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [noShowButton setBackgroundImage:[UIImage imageNamed:@"CPArenaSegmentSelectedBG"] forState:UIControlStateNormal];
    [noShowButton addTarget:self action:@selector(closeButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    //分割线
    UIView *view = [[UIView alloc] init];
    [self.showView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.showView).offset(-2);
        make.centerX.equalTo(self.showView);
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(20);
    }];
    view.backgroundColor = [UIColor blackColor];
    //开启用户交互
//    self.showView.userInteractionEnabled = YES;
}

- (void)closeButtonDidClick {
    [UIView animateWithDuration:0.25 animations:^{
        self.showView.alpha = 0;
        self.coverView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.showView removeFromSuperview];
        [self.coverView removeFromSuperview];
    }];
}
//----------------

//懒加载
- (NSArray *)setArray {
    if (_setArray == nil) {
        NSArray *tempArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"setCell.plist" ofType:nil]];
        NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:tempArray.count];
        for (NSDictionary *dict in tempArray) {
            TZSetCellModel *model = [TZSetCellModel setCellWithDict:dict];
            [mArray addObject:model];
        }
        _setArray = mArray;
    }
    return _setArray;
}
@end
